/*
 * AbstractConstraintValidator.java
 * 
 * Created on 21-Aug-2007, 08:30:25
 * 
 * Copyright (C)
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

package com.grubby.databean.constraints.validation;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * An abstract base class for {@link ConstraintValidator ConstraintValidators}.
 * Provides some common functionality, making it a little easier to implement 
 * validators.
 * <p>
 * Note this has been adapted from an Annotation processing example created by 
 * Anders Holmgren of Sun Microsystems
 * </p>
 * 
 * @author Anders Holmgren, with alterations by A. HARMEL-LAW. 
 * E: andrew.harmel.law@gmail.com
 * @version 0.1
 */
public abstract class AbstractConstraintValidator 
        implements ConstraintValidator {
    
    private Set<Class<? extends Annotation>> constraintTypeSet;

    /**
     * Constructs an AbstractConstraintValidator with the constraintTypes that
     * this validator will support.
     * @param constraintTypes the constraintTypes to support
     * @throws IllegalArgumentException if no constraintType is provided
     */
    protected AbstractConstraintValidator(Class<? extends Annotation>... 
            constraintTypes) {
        if (constraintTypes.length == 0) {
            throw new IllegalArgumentException("must specify at least one " 
                    + "constraint type for the validator");
        }
            
        /*
         * TODO: Figure out why the varargs in the AbstractConstraintValidator 
         * ctr is giving compiler warnings in subclasses
         */
        List<Class<? extends Annotation>> l = Arrays.asList(constraintTypes);
        
        Set<Class<? extends Annotation>> s
                = new HashSet<Class<? extends Annotation>>();
        s.addAll(l);
        constraintTypeSet = Collections.unmodifiableSet(s);
    }

    public Set<Class<? extends Annotation>> getSupportedConstraintTypes() {
        
        for (Class<? extends Annotation> constraint : constraintTypeSet) {
            Logger.getLogger(AbstractConstraintValidator.class.getName())
                    .log(Level.FINE, "constraint: " + constraint.toString());
        }
        
        return constraintTypeSet;
    }

    /**
     * A helper method to ensure constraint is a legal value for this validator.
     * Intended to be called at the beginning of a subclasses 
     * {@link #validate validate} method.
     * @param constraint the constraint value to check
     * @throws IllegalArgumentException if constraint is either null or not one
     * of the constraint types supported by this validator
     */
    protected void ensureConstraintIsSupported(Annotation constraint) {
        if (constraint == null) {
            throw new IllegalArgumentException("null is not a legal value for " 
                    + "constraint");
        }
        
        if (!(constraintTypeSet.contains(constraint.annotationType()))) {
            throw new IllegalArgumentException("the provided constraint is not " 
                    + "supported by this validator");
        }
    }
    
}

